package com.entmanage.base.leave.controller;

import com.entmanage.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.entmanage.common.annotation.Log;
import com.entmanage.common.core.controller.BaseController;
import com.entmanage.common.core.domain.AjaxResult;
import com.entmanage.common.enums.BusinessType;
import com.entmanage.base.leave.domain.LeaveBalance;
import com.entmanage.base.leave.service.ILeaveBalanceService;
import com.entmanage.common.utils.poi.ExcelUtil;
import com.entmanage.common.core.page.TableDataInfo;

/**
 * 员工假期管理Controller
 *
 * @author lsq
 * @date 2025-09-29
 */
@Api(tags = {"leave-LeaveBalance模块"}, description = "员工假期管理")
@RestController
@RequestMapping("/leave/balance")
public class LeaveBalanceController extends BaseController
{
    @Autowired
    private ILeaveBalanceService leaveBalanceService;

    /**
     * 查询员工假期管理列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('leave:balance:list')")
    public TableDataInfo list(LeaveBalance leaveBalance) {
        // 权限控制：非管理员只能查自己
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            Long currentUserId = SecurityUtils.getUserId();
            // 通过 userId 查询对应的 empId
            Long empId = leaveBalanceService.selectEmpIdByUserId(currentUserId);
            if (empId == null) {
                // 如果当前用户没有绑定员工信息，返回空列表
                return getDataTable(new ArrayList<>());

            }
            // 强制设置查询条件为当前员工
            leaveBalance.setEmployeeId(empId);
        }
        // 注意：即使管理员传了 employeeId，也允许（用于筛选）
        startPage();
        List<LeaveBalance> list = leaveBalanceService.selectLeaveBalanceList(leaveBalance);
        return getDataTable(list);
    }

    /**
     * 导出员工假期管理列表
     */
    @PreAuthorize("@ss.hasPermi('leave:balance:export')")
    @Log(title = "员工假期管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaveBalance leaveBalance)
    {
        List<LeaveBalance> list = leaveBalanceService.selectLeaveBalanceList(leaveBalance);
        ExcelUtil<LeaveBalance> util = new ExcelUtil<LeaveBalance>(LeaveBalance.class);
        util.exportExcel(response, list, "员工假期管理数据");
    }

    /**
     * 获取员工假期管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('leave:balance:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(leaveBalanceService.selectLeaveBalanceById(id));
    }

    /**
     * 新增员工假期管理
     */
    @PreAuthorize("@ss.hasPermi('leave:balance:add')")
    @Log(title = "员工假期管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeaveBalance leaveBalance)
    {
        return toAjax(leaveBalanceService.insertLeaveBalance(leaveBalance));
    }

    /**
     * 修改员工假期管理
     */
    @PreAuthorize("@ss.hasPermi('leave:balance:edit')")
    @Log(title = "员工假期管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveBalance leaveBalance)
    {
        return toAjax(leaveBalanceService.updateLeaveBalance(leaveBalance));
    }

    /**
     * 删除员工假期管理
     */
    @PreAuthorize("@ss.hasPermi('leave:balance:remove')")
    @Log(title = "员工假期管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(leaveBalanceService.deleteLeaveBalanceByIds(ids));
    }

    /**
     * 根据员工ID查询假期余额
     */
    @GetMapping("/listByEmpId/{empId}")
    public AjaxResult listByEmpId(@PathVariable Long empId) {
        List<LeaveBalance> list = leaveBalanceService.selectLeaveBalanceByEmployeeId(empId);
        return AjaxResult.success(list);
    }
}
